package com.imy.alscript;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Process;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.imy.virtual.client.core.VirtualCore;
import com.imy.virtual.helper.utils.FileUtils;


import java.io.File;

public class MainActivity extends AppCompatActivity {
  private static final int REQUEST_EXTERNAL_STORAGE = 1;
  private static final String TAG = "MainActivity";
  private static String[] PERMISSIONS_STORAGE = {
      "android.permission.READ_EXTERNAL_STORAGE",
      "android.permission.WRITE_EXTERNAL_STORAGE" };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button) findViewById(R.id.install_btn);
    final TextView textView = (TextView) findViewById(R.id.text_des);
    findViewById(R.id.read_kb).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, KBAPKListActivity.class));
      }

    });
    findViewById(R.id.install_repo_apk).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, AppListActivity.class));
      }
    });
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

        //LoadingActivity.launch(MainActivity.this,"com.imy.ysmap",0);
        startActivity(new Intent(MainActivity.this, InstallAPKActivity.class));
      }
    });

    findViewById(R.id.start_btn).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        LoadingActivity.launch(MainActivity.this, "com.imy.simple", 0);

      }
    });
    findViewById(R.id.end_process).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Process.killProcess(Process.myPid());
      }
    });
    FileUtils.createFileDir(getBaseContext().getFilesDir().getAbsolutePath());

    findViewById(R.id.start_activity).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        String lua = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/lua/";
        Log.i("MainActivity","lua =:"+lua);
        File file = new File(lua);
        if (file.exists()) {
          Log.i("MainActivity","发现Lua文件夹");
        } else {
          Log.i("MainActivity","未发现Lua文件夹");
        }
      }
    });
    //检测一下通知栏权限
    boolean notificationEnabled = PermissionHelper.isNotificationEnabled(MainActivity.this);
    if (!notificationEnabled) {
      CommonUtils.openNotificationSetting(this);
    }
    int permission = ActivityCompat.checkSelfPermission(this,
        "android.permission.WRITE_EXTERNAL_STORAGE");
    if (permission != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
    } else {
      //有权限

    }
    View v = findViewById(R.id.clicked_test);
    v.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(MainActivity.this, "我被点击了", Toast.LENGTH_LONG).show();
        //LuaSupport.get().getLuaCore().stopLua();
      }
    });

    v.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        v.getViewTreeObserver().removeOnGlobalLayoutListener(this);

//        Instrumentation base = ActivityThread.mInstrumentation.get(VirtualCore.mainThread());
//        UiAutomation uiAutomation = base.getUiAutomation();
//        Log.i(TAG,"UiAutomation:"+uiAutomation);

        float x = v.getX();
        float y = v.getY();

        x= v.getWidth()/2;
        //Log.i(TAG,"x:"+x+ "y:"+y );
        //GestureCore.get().clicked(x,y);
      }
    });
    //回调lua
    VirtualCore.get().getLuaService().onMain();
    //LuaSupport.get().getLuaCore().getIAppLoad().onMain();
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_EXTERNAL_STORAGE) {
      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "权限通过");
      } else {
        Log.i(TAG, "权限拒绝");
        finish();
      }
    }
  }
}